﻿using System;

import java.util.Scanner;

public class LoanCalculator
{
    private double loanAmount;  // 贷款总额（万元）
    private int months;         // 贷款时间（月数）
    private double monthlyInterestRate;  // 月利率

    public LoanCalculator(double loanAmount, int months, double annualInterestRate)
    {
        this.loanAmount = loanAmount;
        this.months = months;
        this.monthlyInterestRate = annualInterestRate / 12 / 100;  // 将年利率转换为月利率
    }

    // 等额本金还款法计算总利息
    public double equalPrincipal()
    {
        double totalInterest = 0;
        double monthlyPrincipal = loanAmount / months;  // 每月偿还的本金

        for (int month = 0; month < months; month++)
        {
            double remainingPrincipal = loanAmount - month * monthlyPrincipal;
            double interest = remainingPrincipal * monthlyInterestRate;
            totalInterest += interest;
        }

        return totalInterest;
    }

    // 等额本息还款法计算总利息
    public double equalInstallments()
    {
        double monthlyPayment = loanAmount * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, months)) /
                (Math.pow(1 + monthlyInterestRate, months) - 1);
        double totalRepayment = monthlyPayment * months;
        return totalRepayment - loanAmount;  // 总利息 = 总还款金额 - 贷款总额
    }

    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);

        // 获取用户输入贷款总额、贷款月份和年利率
        System.out.print("Enter loan amount (in 万元): ");
        double loanAmount = scanner.nextDouble();

        System.out.print("Enter loan duration (in months): ");
        int months = scanner.nextInt();

        System.out.print("Enter annual interest rate (in percentage): ");
        double annualInterestRate = scanner.nextDouble();

        // 创建 LoanCalculator 对象并计算总利息
        LoanCalculator loanCalculator = new LoanCalculator(loanAmount, months, annualInterestRate);

        // 输出两种还款方式的总利息
        System.out.println("Total interest for Equal Principal: " + loanCalculator.equalPrincipal());
        System.out.println("Total interest for Equal Installments: " + loanCalculator.equalInstallments());

        scanner.close();
    }
}